package com.naiterui.ehp.bs.consult.controller.inner;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.bo.consult.VideoConsultBO;
import com.naiterui.ehp.bp.bo.consult.VideoConsultMessageBO;
import com.naiterui.ehp.bp.bo.consult.VideoConsultRecordBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.constants.SwaggerTags;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.consult.service.IVideoConsultMessageService;
import com.naiterui.ehp.bs.consult.service.IVideoConsultService;
import com.naiterui.ehp.bs.consult.vo.PatientConsultRecordVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author luao
 * @date 2021/2/26 10:56 上午
 **/
@Slf4j
@RestController
@RequestMapping("/inner/video/consult")
@RequiredArgsConstructor
@Api(tags = SwaggerTag.VIDEO + SwaggerTag.INNER_API + SwaggerTag.VIDEO_CONSULT)
public class InnerVideoConsultController {
    private final IVideoConsultService videoConsultService;
    private final IVideoConsultMessageService videoMessageService;

    /**
     * 根据患者id、医生id获取视频问诊信息
     *
     * @param patientId
     * @param doctorId
     * @return
     */
    @GetMapping("/getVideoConsult")
    public VideoConsultBO getVideoConsult(Long patientId, Long doctorId) {
        return videoConsultService.getVideoConsult(patientId, doctorId);
    }

    /**
     * 根据患者id、医生id获取视频问诊信息
     *
     * @return
     */
    @PostMapping("/getVideoConsults")
    public List<VideoConsultBO> getVideoConsult(@RequestBody List<Long> videoConsultIds) {
        return videoConsultService.getVideoConsults(videoConsultIds);
    }


    /**
     * 根据id获取视频问诊信息
     *
     * @param videoConsultId
     * @return
     */
    @GetMapping("/getVideoConsultById")
    public VideoConsultBO getVideoConsult(Long videoConsultId) {
        return videoConsultService.getVideoConsultById(videoConsultId);
    }

    /**
     * 根据订单号查询
     *
     * @param orderSn
     * @return
     */
    @GetMapping("/getVideoConsultByOrderSn")
    public VideoConsultBO getVideoConsultByOrderSn(String orderSn) {
        return videoConsultService.getVideoConsultByOrderSn(orderSn);
    }

    @PostMapping("saveOrUpdate")
    public VideoConsultBO saveOrUpdate(@RequestBody VideoConsultBO videoConsultBO) {
        return videoConsultService.saveOrUpdate(videoConsultBO);
    }

    @PostMapping("cancel")
    public void cancel(Long videoConsultId) throws BusinessException {
        videoConsultService.cancelVideoConsult(videoConsultId);
    }

    @GetMapping("/getConsultVideoListByPatientId")
    @ApiOperation(value = "小程序首页患者视频问诊记录", tags = {SwaggerTags.WX_VIDEO_CONSULT})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "patientId", value = "患者id", dataType = "long", paramType = "query")
    })
    public ResponseVO<PageVO<VideoConsultRecordBO>> consultVideoList(Long patientId, int page, int num) throws BusinessException {
        log.info("小程序首页患者视频问诊记录 patientId={},page={},num={}", patientId, page, num);
        return PublicService.returnResponseVO(videoConsultService.consultVideoList(patientId, page, num));
    }

    /**
     * （医生端1.1优化）根据问诊状态查询数量
     * @param doctorId
     * @return
     */
    @GetMapping("/status/count")
    @ApiOperation(value = "根据问诊状态查询数量", tags = {SwaggerTags.APP_VIDEO_CONSULT, SwaggerTags.CMS_VIDEO_CONSULT})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生id", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "videoConsultStatus", value = "状态", required = true, dataType = "int", paramType = "query")
    })
    public ResponseVO<Integer> getCountByStatus(@RequestParam("doctorId") Long doctorId,@RequestParam("status") Integer videoConsultStatus) throws BusinessException  {
        log.info("根据医生id和问诊状态查询视频问诊数量 doctorId={}，videoConsultStatus={}",doctorId , videoConsultStatus);
        return PublicService.returnResponseVO(videoConsultService.getCountByStatus(doctorId,videoConsultStatus));
    }

    /**
     * （医生端1.1优化）
     * 根据医生id获取视频问诊订单
     *
     * @param doctorId
     * @return
     */
    @PostMapping("/getOrderByDoctorId")
    public List<VideoConsultBO> getOrderByDoctorId(@RequestParam Long doctorId) throws BusinessException {
        log.info("根据医生id获取视频问诊订单 doctorId={}",doctorId);
        return videoConsultService.getOrderByDoctorId(doctorId);
    }

    @GetMapping("/getVideoConsultsPage")
    public PageVO<PatientConsultRecordVO> getVideoConsultsPage(PageParamVO page,
        @RequestParam(value = "inquirerIds") List<Long> inquirerIds) {
        return videoConsultService.getFinishedVideoConsultsPage(page, inquirerIds);
    }

    @GetMapping("/getPatientConsultRecord")
    public PatientConsultRecordVO getPatientConsultRecord(Long videoConsultId) {
        return videoConsultService.getPatientConsultRecord(videoConsultId);
    }

    @GetMapping("/getMessageByVideoConsultId")
    public List<VideoConsultMessageBO> getMessageByVideoConsultIds(Long videoConsultId,
        @RequestParam(value = "types") List<Integer> types) {
        return videoMessageService.getMessageByVideoConsultId(videoConsultId, types);
    }
}
